package com.syn.synold4s.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import androidx.core.content.ContextCompat;
import com.gaoyi.basemodule.baseAdapter.BaseAdapter;
import com.gaoyi.basemodule.baseAdapter.BaseViewHolder;
import com.gaoyi.basemodule.baseUtil.DateFormatUtil;
import com.gaoyi.basemodule.baseUtil.DensityUtil;
import com.gaoyi.basemodule.baseUtil.LanguageUtils;
import com.syn.synold4s.R;
import com.syn.synold4s.bean.dto.PermitToWorkDto;
import com.syn.synold4s.databinding.PermitToWorkItemBinding;
import java.util.Date;

public class PermitToWorkAdapter extends BaseAdapter<PermitToWorkItemBinding, PermitToWorkDto> {

    private Integer mLastPosition;
    private boolean isMain = true;

    public PermitToWorkAdapter(Context context) {
        super(context);
    }

    public void setMain(boolean main) {
        isMain = main;
    }

    @Override
    protected void setBindingVariable(PermitToWorkItemBinding binding, int position) {

    }

    @Override
    protected void onBindItem(PermitToWorkItemBinding binding, PermitToWorkDto item, int position) {
        if (isMain) {
            ViewGroup.LayoutParams params = binding.llPtw.getLayoutParams();
            if (getItemCount() >= 3) {
                params.width = DensityUtil.dp2px(170);
            } else {
                params.width = ViewGroup.LayoutParams.MATCH_PARENT;
            }
            binding.llPtw.setLayoutParams(params);
        }
        if (LanguageUtils.getInstance().isEnglish()) {
            binding.tvPtwTitle.setText(item.getCode() != null ? item.getCode() : "");
        } else {
            binding.tvPtwTitle.setText(item.getName() != null ? item.getName() : "");
        }
        Drawable mDrawable = ContextCompat.getDrawable(mContext, R.drawable.ic_risc);
        if (mDrawable != null) {
            mDrawable.setBounds(0, 0, mDrawable.getIntrinsicWidth(), mDrawable.getIntrinsicHeight());
        }
        if (null != item.getCode()) {
            if ("RFI".equals(item.getCode()) || "RISC".equals(item.getCode())) {
                mDrawable = ContextCompat.getDrawable(mContext, R.drawable.ic_risc);
            } else if ("SecurityCheck".equals(item.getCode())) {
                mDrawable = ContextCompat.getDrawable(mContext, R.drawable.ic_security_check);
            } else if ("Cleaning inspection".equals(item.getCode())) {
                mDrawable = ContextCompat.getDrawable(mContext, R.drawable.ic_cleaning_inspection);
            }
        }
        if (mDrawable != null) {
            mDrawable.setBounds(0, 0, mDrawable.getIntrinsicWidth(), mDrawable.getIntrinsicHeight());
            binding.tvPtwTitle.setCompoundDrawablesRelative(null, null, null, mDrawable);
        } else {
            binding.tvPtwTitle.setCompoundDrawablesRelative(null, null, null, null);
        }
        binding.tvPtwTime.setText(DateFormatUtil.DateTimeToStr(new Date(), "dd-MM-YYYY HH:mm:ss"));
        binding.tvPtwApproval.setText(String.valueOf(item.getApprovalTotal()));
        binding.tvPtwApprovaled.setText(String.valueOf(item.getApprovedTotal()));
        binding.rlPtw.setVisibility(item.isCheck() ? View.VISIBLE : View.INVISIBLE);
        ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) binding.llPtw.getLayoutParams();
        if (isMain) {
            if (position == getItemCount() - 1) {
                layoutParams.setMargins(0, 0, 0, 0);
            } else {
                layoutParams.setMargins(0, 0, DensityUtil.dp2px(1), 0);
            }
        } else {
            layoutParams.setMargins(DensityUtil.dp2px(8), DensityUtil.dp2px(8), 0, 0);
        }
    }

    public void changeSelect(int position) {
        boolean check = getCurrentList().get(position).isCheck();
        if (mLastPosition == null) {
            mLastPosition = position;
        } else {
            if (mLastPosition == position) {
                if (check) {
                    mLastPosition = null;
                }
            } else {
                try {
                    getCurrentList().get(mLastPosition).setCheck(false);
                    notifyItemChanged(mLastPosition);
                    mLastPosition = position;
                } catch (IndexOutOfBoundsException e) {
                    e.printStackTrace();
                }
            }
        }
        getCurrentList().get(position).setCheck(!check);
        notifyItemChanged(position);
    }

    @Override
    protected void onCreateView(PermitToWorkItemBinding binding, BaseViewHolder<PermitToWorkItemBinding> holder) {

    }

    @Override
    protected int getLayoutResId() {
        return R.layout.permit_to_work_item;
    }
}
